﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TaskManager
{
    public partial class ClockForm : Form
    {
        public ClockForm()
        {
            InitializeComponent();
        }

        public ClockForm(string mode)
        {
            Mode = mode;
            InitializeComponent();
            this.Text = Mode;
        }

        public string Mode = "提醒信息记录";      // 界面类型信息

        // -------------------------------------
        public static ClockForm RecordForm;
        private static List<Data_Remind> record;

        // 获取信息记录展示界面
        public static ClockForm getRecordForm(List<Data_Remind> recordData)
        {
            record = recordData;
            if (RecordForm == null) RecordForm = new ClockForm("提醒信息记录");
            return RecordForm;
        }

        // -------------------------------------
        public static ClockForm MessageForm;
        private static List<Data_Remind> message;

        // 获取信息记录展示界面
        public static ClockForm getRemindForm(List<Data_Remind> messageData)
        {
            message = messageData;
            if (MessageForm == null) MessageForm = new ClockForm("信息");
            return MessageForm;
        }

        // -------------------------------------
        public static ClockForm TaskForm;
        private static List<Data_Task> task;

        // 获取任务信息展示界面
        public static ClockForm getTaskForm(List<Data_Task> taskList)
        {
            task = taskList;        // 设置任务信息列表
            if (TaskForm == null) TaskForm = new ClockForm("任务信息");
            
            return TaskForm;
        }

        // -------------------------------------
        public static ClockForm DemandForm;
        private static List<Data_Demand> demand;

        // 获取任务信息展示界面
        public static ClockForm getDemandForm(List<Data_Demand> taskList)
        {
            demand = taskList;        // 设置任务信息列表
            if (DemandForm == null) DemandForm = new ClockForm("需求信息");

            return DemandForm;
        }

        // -------------------------------------
        public static ClockForm BugForm;
        private static List<Data_Bug> Bug;

        // 获取Bug信息展示界面
        public static ClockForm getBugForm(List<Data_Bug> BugList)
        {
            Bug = BugList;        // 设置任务信息列表
            if (BugForm == null) BugForm = new ClockForm("Bug信息");

            return BugForm;
        }

        // -------------------------------------
        public static ClockForm TesttaskForm;
        private static List<Data_Testtask> Testtask;

        // 获取Testtask信息展示界面
        public static ClockForm getTesttaskForm(List<Data_Testtask> TesttaskList)
        {
            Testtask = TesttaskList;        // 设置任务信息列表
            if (TesttaskForm == null) TesttaskForm = new ClockForm("测试信息");

            return TesttaskForm;
        }


        private void clockForm_Load(object sender, EventArgs e)
        {
            FormTool.RestoreFormInfo(this);

            if (Mode == "提醒信息记录")Tool.setData(listView, record);           // 在界面中展示
            else if (Mode == "信息") Tool.setData(listView, message);            // 在界面中展示
            else if (Mode == "任务信息") Tool.setData(listView, task);           // 在界面中展示
            else if (Mode == "需求信息") Tool.setData(listView, demand);         // 在界面中展示
            else if (Mode == "Bug信息") Tool.setData(listView, Bug);             // 在界面显示bug信息
            else if (Mode == "测试信息") Tool.setData(listView, Testtask);       
        }

        private void listView_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listView.SelectedItems.Count > 0)
            {
                int index = listView.SelectedIndices[0];
                if (index != -1)
                {
                    //MessageForm.ShowRecord(iteam);
                    if (Mode == "提醒信息记录") ShowRecord(record[index]);
                    else if (Mode == "信息") ShowMessage(message[index]);
                    else if (Mode == "任务信息") ShowTask(task[index]);
                    else if (Mode == "需求信息") ShowDemand(demand[index]);
                    else if (Mode == "Bug信息") ShowBug(Bug[index]);
                    else if (Mode == "测试信息") ShowTesttask(Testtask[index]);
                }
            }
        }

        private void ClockForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (Mode == "提醒信息记录") RecordForm = null;
            else if (Mode == "信息") MessageForm = null;
            else if (Mode == "任务信息") TaskForm = null;
            else if (Mode == "需求信息") DemandForm = null;
            else if (Mode == "Bug信息") BugForm = null;
            else if (Mode == "测试信息") TesttaskForm = null;

            
            FormTool.saveFormInfo(this);
        }


        //--------------------------------        
        // 显示一个消息提示弹窗
        public void Show(string tittle, string content, string sender, string id)
        {
            label_tittle.Text = tittle;
            textBox_content.Text = "\r\n" + content;
            label_sender.Text = sender;

            //remindId = id; // 记录提示的任务信息id
        }

        //--------------------------------
        // 显示一个消息记录
        public void ShowRecord(string tittle, string content, string sender, string id, string sendtime)
        {
            Show("(" + sendtime + ")" + tittle, content, sender, id);
        }

        // 显示一个消息记录
        public void ShowRecord(Data_Remind remind)
        {
            string sender = remind.uaccount.Equals(remind.urealname) ? remind.uaccount : (remind.uaccount + "(" + remind.urealname + ")");
            ShowRecord(remind.title, remind.content, sender, remind.id, remind.sendTime);
        }

        //--------------------------------
        // 显示一个消息
        public void ShowMessage(Data_Remind remind)
        {
            string sender = remind.uaccount.Equals(remind.urealname) ? remind.uaccount : (remind.uaccount + "(" + remind.urealname + ")");
            ShowRecord(remind.title, remind.content, sender, remind.id, remind.sendTime);

            loginForm.Remind_Ids.saveIdData(remind.id);
            loginForm.savePeriodRemind_IdData(remind.id);   // 记录当前提示的周期性提示信息状态
        }


        //--------------------------------
        // 显示一个任务消息
        public void ShowTask(string tittle, string content, string sender, string id, string assignedate, string assignedTo, string status)
        {
            Show(tittle + " (指派给：" + assignedTo + ") - " + status, content, sender, id);
        }

        // 显示一个任务消息
        public void ShowTask(Data_Task task)
        {
            //string sender = remind.uaccount.Equals(remind.urealname) ? remind.uaccount : (remind.uaccount + "(" + remind.urealname + ")");
            ShowTask(task.name, task.desc, task.openedBy, task.id, task.assignedDate, task.assignedTo, task.status);

            loginForm.Task_Ids.saveIdData(task.id);     // 记录当前的任务状态信息
        }


        //--------------------------------
        // 显示一个需求信息
        public void ShowDemand(string tittle, string content, string sender, string id, string assignedate, string assignedTo)
        {
            Show(tittle + " (指派给：" + assignedTo + ", " + assignedate + ")", content, sender, id);
        }

        // 显示一个需求信息
        public void ShowDemand(Data_Demand demand)
        {
            //string sender = remind.uaccount.Equals(remind.urealname) ? remind.uaccount : (remind.uaccount + "(" + remind.urealname + ")");
            ShowDemand(demand.title, demand.title, demand.openedBy, demand.id, demand.assignedDate, demand.assignedTo);

            loginForm.Demand_Ids.saveIdData(demand.id);       // 记录当前显示信息的id
        }

        //--------------------------------
        // 显示一个Bug信息
        public void ShowBug(string tittle, string content, string sender, string id, string assignedate, string assignedTo, string status)
        {
            Show(tittle + " (指派给：" + assignedTo + ") - " + status, content, sender, id);
        }

        // 显示一个Bug信息
        public void ShowBug(Data_Bug bug)
        {
            //string sender = remind.uaccount.Equals(remind.urealname) ? remind.uaccount : (remind.uaccount + "(" + remind.urealname + ")");
            ShowTask(bug.title, bug.steps, bug.openedBy, bug.id, bug.assignedDate, bug.assignedTo, bug.status);

            loginForm.Bug_Ids.saveIdData(bug.id);       // 记录当前显示信息的id
        }

        //--------------------------------
        // 显示一个Testtask信息
        public void ShowTesttask(string tittle, string content, string sender, string id, string assignedate, string assignedTo, string status)
        {
            Show(tittle + " (指派给：" + assignedTo + ") - " + status, content, sender, id);
        }

        // 显示一个Testtask信息
        public void ShowTesttask(Data_Testtask Testtask)
        {
            ShowTesttask(Testtask.name, Testtask.desc, Testtask.build, Testtask.id, Testtask.begin, Testtask.owner, Testtask.status);

            loginForm.Testtask_Ids.saveIdData(Testtask.id);       // 记录当前显示信息的id
        }
                


        // 双击信息项时，跳转到信息对应的网页详情页
        private void listView_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (listView.SelectedItems.Count > 0)
            {
                int index = listView.SelectedIndices[0];
                if (index != -1)
                {
                    //MessageForm.ShowRecord(iteam);
                    if (Mode == "提醒信息记录") ShowRecord(record[index]);
                    else if (Mode == "信息") ShowMessage(message[index]);
                    else if (Mode == "任务信息") TaskManage.ShowTaskOnBroswer(task[index].id);
                    else if (Mode == "需求信息") ShowDemand(demand[index]);
                    //else if (Mode == "Bug信息") BugForm = null;
                    //else if (Mode == "测试信息") TesttaskForm = null;
                }
            }

            //if (Mode == "提醒信息记录") RecordForm = null;
            //else if (Mode == "信息") MessageForm = null;
            //else if (Mode == "任务信息") TaskForm = null;
            //else if (Mode == "需求信息") DemandForm = null;
        }


    }
}
